/* 就诊卡模块 */
import _ from 'underscore'
import {
  ADD_WARDS,
  DELETE_WARD,
  SET_DEFAULT_WARD,
  SELECT_WARD
} from '../mutation-types'

// initial state
const state = {
  // ward object {
  //   id: 0,
  //   type: '',
  //   num: '',
  //   name: '',
  //   isDefault: false,
  //   isSelected: false
  // }
  wards: []
}

// mutations
const mutations = {
  [ADD_WARDS] (state, wards) {
    for (var i = 0; i < wards.length; i++) {
      var ward = wards[i]
      state.wards.push(ward)
    }
    _.uniq(state.wards, function (val) {
      return val.id
    })
  },
  [DELETE_WARD] (state, wardId) {
    state.wards = _.filter(state.wards,
      (ward) => {
        return ward.id !== wardId
      })
  },
  [SET_DEFAULT_WARD] (state, wardId) {
    _.each(state.wards, (ward) => {
      if (ward.id === wardId) {
        ward.isDefault = true
      } else {
        ward.isDefault = false
      }
    })
  },
  [SELECT_WARD] (state, wardId) {
    _.each(state.wards, (ward) => {
      if (ward.id === wardId) {
        ward.isSelected = true
      } else {
        ward.isSelected = false
      }
    })
  }
}

export default {
  state,
  mutations
}
